package com.leosoft.heatmonitor.service.transactional;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.leosoft.heatmonitor.dao.DataHeatDAO;
import com.leosoft.heatmonitor.dao.UserDAO;
import com.leosoft.heatmonitor.model.Role;
import com.leosoft.heatmonitor.model.User;

@Component("dataHeatService")
public class DataHeatService {
	@Autowired
	private DataHeatDAO dataHeatDAO;
	@Autowired
	private UserDAO userDAO;

	public Map<String, Object> list(int startIndex, int length,
			List<String[]> wherePart, List<String[]> odrerPart,
			String startTime, String endTime, String username) {
		User user = userDAO.getUserByName(username);
		if (!user.getRoles().isEmpty()) {
			if (user.getRoles().get(0).getRole().equals("PM")) {
				return dataHeatDAO.list(startIndex, length, wherePart, odrerPart,
						startTime, endTime, user.getId());
			} else {
				return dataHeatDAO.list(startIndex, length, wherePart, odrerPart,
						startTime, endTime, "");
			}
		}

		return null;
	}

	public List<Map<String, String>> trends(String projectId, String start,
			String end, String username, String dataName) {
		User user = userDAO.getUserByName(username);
		if (!user.getRoles().isEmpty()) {
			if (user.getRoles().get(0).getRole().equals("PM")) {
				return dataHeatDAO.trends(projectId, start, end, user.getId(),
						dataName);
			} else {
				return dataHeatDAO.trends(projectId, start, end, "", dataName);
			}
		}
		return null;
	}

	public List<Map<String, String>> dArea(String projectId, String building,
			String door, String username, String dataName) {
		User user = userDAO.getUserByName(username);
		if (!user.getRoles().isEmpty()) {
			if (user.getRoles().get(0).getRole().equals("PM")) {
				return dataHeatDAO.dArea(projectId, building, door, user.getId(),
						dataName);
			} else {
				return dataHeatDAO.dArea(projectId, building, door, "", dataName);
			}
		}
		return null;
	}

	public List<List<Map<String, String>>> temperatureDArea(String projectId,
			String building, String door, String username) {
		User user = userDAO.getUserByName(username);
		if (!user.getRoles().isEmpty()) {
			if (user.getRoles().get(0).getRole().equals("PM")) {
				return dataHeatDAO.temperatureDArea(projectId, building, door,
						user.getId());
			} else {
				return dataHeatDAO.temperatureDArea(projectId, building, door, "");
			}
		}
		return null;
	}

	public List<Map<String, String>> area(String projectId, String building,
			String username, String dataName) {
		User user = userDAO.getUserByName(username);
		if (!user.getRoles().isEmpty()) {
			if (user.getRoles().get(0).getRole().equals("PM")) {
				return dataHeatDAO
						.area(projectId, building, user.getId(), dataName);
			} else {
				return dataHeatDAO.area(projectId, building, "", dataName);
			}
		}
		return null;
	}

	public Map<String, Object> dashBoard(int startIndex, int length,
			List<String[]> wherePart, List<String[]> odrerPart, String username) {
		User user = userDAO.getUserByName(username);
		if (!user.getRoles().isEmpty()) {
			if (user.getRoles().get(0).getRole().equals("PM")) {
				return dataHeatDAO.dashBoard(startIndex, length, wherePart,
						odrerPart, user.getId());
			} else {
				return dataHeatDAO.dashBoard(startIndex, length, wherePart,
						odrerPart, "");
			}
		}

		return null;
	}

	public Map<String, Object> monitorList(int startIndex, int length,
			List<String[]> wherePart, List<String[]> odrerPart,
			String startTime, String endTime, String username) {
		User user = userDAO.getUserByName(username);
		List<Role> roles = user.getRoles();
		if (!roles.isEmpty()) {
			if (roles.get(0).getRole().equals("PM")) {
				return dataHeatDAO.monitorList(startIndex, length, wherePart,
						odrerPart, startTime, endTime, user.getId());
			} else {
				return dataHeatDAO.monitorList(startIndex, length, wherePart,
						odrerPart, startTime, endTime, "");
			}
		}

		return null;
	}

	public boolean check(String username, String old) {
		User user = userDAO.getUserByName(username);
		String oldP = encryption(old);
		if (user.getPassword().equals(oldP)) {
			return true;
		}
		return false;
	}

	public void changePwd(String username, String newp) {
		User user = userDAO.getUserByName(username);
		user.setPassword(encryption(newp));
		userDAO.saveUser(user);
	}

	public String encryption(String plainText) {
		String re_md5 = new String();
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}

			re_md5 = buf.toString();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return re_md5;
	}
}
